package com.api.phm.domain.query;

import com.alibaba.fastjson2.JSONObject;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.api.common.language.LanguageBean;
import lombok.Data;
import org.apache.commons.lang3.StringUtils;

import java.util.Date;

@Data
public class FaultLogPageQuery {
    private String faultId;
    private Long plcId;
    private String plcName;
    @JsonIgnore
    private JSONObject localPlcName;
    private Long lineId;
    private String lineName;
    @JsonIgnore
    private JSONObject localLineName;
    private Long robotId;
    private String robotName;
    @JsonIgnore
    private JSONObject localRobotName;
    private Long elementId;
    private String elementName;
    @JsonIgnore
    private JSONObject localElementName;
    private String datCol;
    private String datColName;
    @JsonIgnore
    private String datColLabel;
    //    private String datColNameJson;
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private Date startTime;
    private String faultLevel;
    private String faultLevelDesc;
    @JsonIgnore
    private String faultLevelLabel;
    private String faultCode;
    private String faultStatus;
    private String faultStatusDesc;
    @JsonIgnore
    private String faultStatusLabel;
    private String faultMessage;
    private String faultMessageLabel;
    @JsonIgnore
    private JSONObject triggerValue;
    private String readMark;
    private String readMarkDesc;
    @JsonIgnore
    private String readMarkLabel;
    @JsonIgnore
    private String appLang;


    public void setLocalRobotName(String localRobotName) {
        if (StringUtils.isNotBlank(localRobotName)) {
            this.localRobotName = JSONObject.parseObject(localRobotName);
        } else {
            this.localRobotName = null;
        }
    }

    public void setLocalPlcName(String strLocalPlcName) {
        if (StringUtils.isNotBlank(strLocalPlcName)) {
            this.localPlcName = JSONObject.parseObject(strLocalPlcName);
        } else {
            this.localPlcName = null;
        }
    }

    public void setLocalLineName(String strLocalLineName) {
        if (StringUtils.isNotBlank(strLocalLineName)) {
            this.localLineName = JSONObject.parseObject(strLocalLineName);
        } else {
            this.localLineName = null;
        }
    }

    public void setLocalElementName(String localElementName) {
        if (StringUtils.isNotBlank(localElementName)) {
            this.localElementName = JSONObject.parseObject(localElementName);
        } else {
            this.localElementName = null;
        }
    }

    public void setTriggerValue(String triggerValue) {
        if (StringUtils.isNotBlank(triggerValue)) {
            this.triggerValue = JSONObject.parseObject(triggerValue);
        } else {
            this.triggerValue = null;
        }
    }

//    public void setDatColLabel(String datColLabel) throws Exception {
//        this.datColLabel = datColLabel;
//        if (StringUtils.isNotBlank(datColLabel)) {
//            String strAppLang = StringUtils.isNotBlank(appLang) ? appLang : LanguageBean.getInitLang();
//            this.datColName = LanguageBean.getLangJsonObj(strAppLang, datColLabel);
//        }
//    }
//
//    public void setFaultLevelLabel(String faultLevelLabel) throws Exception {
//        this.faultLevelLabel = faultLevelLabel;
//        if (StringUtils.isNotBlank(faultLevelLabel)) {
//            String strAppLang = StringUtils.isNotBlank(appLang) ? appLang : LanguageBean.getInitLang();
//            this.faultLevelDesc = LanguageBean.getLangJsonObj(strAppLang, faultLevelLabel);
//        }
//    }

//    public void setFaultStatusLabel(String faultStatusLabel) throws Exception {
//        this.faultStatusLabel = faultStatusLabel;
//        if (StringUtils.isNotBlank(faultStatusLabel)) {
//            String strAppLang = StringUtils.isNotBlank(appLang) ? appLang : LanguageBean.getInitLang();
//            this.faultStatusDesc = LanguageBean.getLangJsonObj(strAppLang, faultStatusLabel);
//        }
//    }

//    public void setReadMarkLabel(String readMarkLabel) throws Exception {
//        this.readMarkLabel = readMarkLabel;
//        if (StringUtils.isNotBlank(readMarkLabel)) {
//            String strAppLang = StringUtils.isNotBlank(appLang) ? appLang : LanguageBean.getInitLang();
//            this.readMarkDesc = LanguageBean.getLangJsonObj(strAppLang, readMarkLabel);
//        }
//    }

//    public void setTriggerValue(String triggerValue) {
//        this.triggerValue = triggerValue;
//        String message = faultMessage;
//        try {
//            System.out.println(appLang);
//            System.out.println(getAppLang());
//            String strAppLang = StringUtils.isNotBlank(appLang) ? appLang : LanguageBean.getInitLang();
//            message = LanguageBean.getLangJsonObj(strAppLang, faultMessageLabel);
//        } catch (Exception e) {
//
//        }
//        faultMessage = message;
//    }


    public void initLabelValue() throws Exception {
        String strAppLang = StringUtils.isNotBlank(appLang) ? appLang : LanguageBean.getInitLang();
        if (StringUtils.isNotBlank(datColLabel)) {
            this.datColName = LanguageBean.getLangJsonObj(strAppLang, datColLabel);
//            System.out.println(this.datColName);
        }
        if (StringUtils.isNotBlank(faultLevelLabel)) {
            this.faultLevelDesc = LanguageBean.getLangJsonObj(strAppLang, faultLevelLabel);
        }
        if (StringUtils.isNotBlank(faultStatusLabel)) {
            this.faultStatusDesc = LanguageBean.getLangJsonObj(strAppLang, faultStatusLabel);
        }
        if (StringUtils.isNotBlank(readMarkLabel)) {
            this.readMarkDesc = LanguageBean.getLangJsonObj(strAppLang, readMarkLabel);
        }
//        String message = faultMessage;
        faultMessage = LanguageBean.getLangJsonObj(strAppLang, faultMessageLabel);

        if (ObjectUtils.isNotNull(lineId)) {
            if (triggerValue.containsKey("lineName")) {
                triggerValue.put("lineName", localLineName.get(strAppLang));
            }
        }
        ;
        if (ObjectUtils.isNotNull(plcId)) {
            if (triggerValue.containsKey("plcName")) {
                triggerValue.put("plcName", localPlcName.get(strAppLang));
            }
        }
        ;
        if (ObjectUtils.isNotNull(robotId)) {
            if (triggerValue.containsKey("robotName")) {
                triggerValue.put("robotName", localRobotName.get(strAppLang));
            }
        }
        ;
        if (ObjectUtils.isNotNull(elementId)) {
            if (triggerValue.containsKey("elementName")) {
                triggerValue.put("elementName", localElementName.get(strAppLang));
            }
        }
        ;
        if (triggerValue.containsKey("datColName")) {
            triggerValue.put("datColName", this.datColName);
        }
        String trend = LanguageBean.getLangJsonObj(strAppLang, ((triggerValue.getFloat("ratio") > 0) ? "trend.up" : "trend.down"));
        triggerValue.put("trend", trend);
        for (String key : triggerValue.keySet()) {
            String old = "${" + key + "}";
            if (faultMessage.indexOf(old) > -1) {
                faultMessage = faultMessage.replace(old, triggerValue.getString(key));
            }
        }
    }
}
